package conceptmaniax.hotelapp.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuItem;
import conceptmaniax.hotelapp.activities.R;
import conceptmaniax.hotelapp.config.Constants;
import conceptmaniax.hotelapp.enums.Language;

public class GeneralUtils {

	public static Language getLanguage(final Context context) {
		final SharedPreferences settings = context.getSharedPreferences(Constants.APP_NAME,0);
		return Language.valueOf(settings.getString(Constants.LANGUAGE, Language.ENGLISH.toString()));
	}
	
	public static void displayAlertDialogWithMessage(final Context context, final String message) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		final AlertDialog dialog;
		String close = null;
		
		Language language = getLanguage(context);
		switch (language) {
		case ENGLISH:
			close = context.getString(R.string.close_en);
			break;
		case GREEK:
			close = context.getString(R.string.close_gr);
			break;
		}
		
		builder.setMessage(message);
		builder.setNegativeButton(close, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(final DialogInterface dialog, final int which) {
				dialog.cancel();
			}
		});
		dialog = builder.create();
		dialog.show();
	}
	
	public static void displaySyncUnsuccessfulMessage(final Context context) {
		String message = null;
		Language language = getLanguage(context);
		switch (language) {
		case ENGLISH:
			message = context.getResources().getString(R.string.sync_unsuccessful_en);
			break;
		case GREEK:
			message = context.getResources().getString(R.string.sync_unsuccessful_gr);
			break;
		}
		GeneralUtils.displayAlertDialogWithMessage(context, message);
	}
	
	public static boolean isAppInstalled(final Context context, final String uri) {
	    PackageManager pm = context.getApplicationContext().getPackageManager();
	    boolean app_installed = false;
	    try {
	        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
	        app_installed = true;
	    } catch (PackageManager.NameNotFoundException e) {
	        app_installed = false;
	    }
	    return app_installed;
	} 
	
	public static String getNoNetworkMessage(final Context context) {
		String message = "";
		switch (getLanguage(context)) {
		case ENGLISH:
			message = context.getString(R.string.no_network_en);
			break;
		case GREEK:
			message = context.getString(R.string.no_network_gr);
			break;
		}
		
		return message;
	}
	
	public static boolean isOnline(final Context context) {
	    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    if (cm != null && ( (cm.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) || 
	    		(cm.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)) ) {
	        return true;
	    }
	    return false;
	}
	
	public static String normilizeUrl(final String url) {
		if ( url == null ) {
			return null;
		}
		
		return url.startsWith("http")? url : "http://" + url; 
	}
	
	public static void openWebPage(final Context context, String url) {
		if (!isOnline(context)) {
			GeneralUtils.displayAlertDialogWithMessage(context, getNoNetworkMessage(context));
			return;
		}
 	}
	
	public static void openWebPageGooglePlay(final Context context) {
		if (!isOnline(context)) {
			GeneralUtils.displayAlertDialogWithMessage(context, getNoNetworkMessage(context));
			return;
		}
		
		final Intent webIntent = new Intent(Constants.WEB_VIEW_ACTIVITY); 
    	webIntent.putExtra("URL", Constants.APP_GOOGLE_PLAY_WEB);
    	context.startActivity(webIntent);
	}
	
	public static void openGooglePlayForApp(final Context context, final String googlePlayApp, final String googlePlayWeb) {
		if (!isOnline(context)) {
			GeneralUtils.displayAlertDialogWithMessage(context, getNoNetworkMessage(context));
			return;
		}
    	
		try {
			final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + googlePlayApp));
			context.startActivity(intent);
		} catch (Exception e) {
			final Intent webIntent = new Intent(Constants.WEB_VIEW_ACTIVITY); 
	    	webIntent.putExtra("URL", googlePlayWeb);
	    	context.startActivity(webIntent);
		}
	}
	
	public static void openSlideMeForApp(final Context context, final String slideMeAppId, final String slideMeWeb) {
		if (!isOnline(context)) {
			GeneralUtils.displayAlertDialogWithMessage(context, getNoNetworkMessage(context));
			return;
		}
    	
		try {
			final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sam://details?bundleId=" + slideMeAppId));
			context.startActivity(intent);
		} catch (Exception e) {
			final Intent webIntent = new Intent(Constants.WEB_VIEW_ACTIVITY); 
	    	webIntent.putExtra("URL", slideMeWeb);
	    	context.startActivity(webIntent);
		}
	}
	
	public static Menu createOptionsMenu(Activity activity, Menu menu) {
		final String rateLabel;
		final String aboutLabel;
		final String exitLabel;
		
		Language language = getLanguage(activity);
		switch (language) {
		case ENGLISH:
			rateLabel = activity.getString(R.string.menu_rate_label_en);
			aboutLabel = activity.getString(R.string.menu_about_label_en);
			exitLabel = activity.getString(R.string.menu_exit_label_en);
			break;
		case GREEK:
			rateLabel = activity.getString(R.string.menu_rate_label_gr);
			aboutLabel = activity.getString(R.string.menu_about_label_gr);
			exitLabel = activity.getString(R.string.menu_exit_label_gr);
			break;
		default:
			rateLabel = activity.getString(R.string.menu_rate_label_en);
			aboutLabel = activity.getString(R.string.menu_about_label_en);
			exitLabel = activity.getString(R.string.menu_exit_label_en);
			break;
		}
				
		menu.add(0,0,0,rateLabel).setIcon(R.drawable.ic_menu_rate);
		menu.add(0,1,1,aboutLabel).setIcon(R.drawable.ic_menu_about);
		menu.add(0,2,2,exitLabel).setIcon(R.drawable.ic_menu_exit);
		return menu;
	}
	
	public static void onOptionsItemSelected(Activity activity, MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			GeneralUtils.openWebPageGooglePlay(activity);
			break;
		case 1:
			activity.showDialog(Constants.DIALOG_ABOUT);
			break;
		case 2:
			activity.moveTaskToBack(true);
			break;
		}
	}
	
	public static Dialog createAboutUsDialog(Activity activity) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		String close = null;
		Language language = getLanguage(activity);
		switch (language) {
			case ENGLISH:
				builder.setMessage(R.string.menu_about_text_en);
				builder.setTitle(R.string.menu_about_title_en);
				close = activity.getString(R.string.close_en);
				break;
			case GREEK:
				builder.setMessage(R.string.menu_about_text_gr);
				builder.setTitle(R.string.menu_about_title_gr);
				close = activity.getString(R.string.close_gr);
				break;
		}
				
		builder.setNegativeButton(close, new DialogInterface.OnClickListener() {	
			@Override
			public void onClick(final DialogInterface dialog, final int which) {
				dialog.cancel();
			}
		});
		
		return builder.create();
	}
}
